package April._20.answer.work4;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        new CopyFile("D:\\temp\\a.jpg", "aa.jpg").start();
        new CopyFile("D:\\temp\\b.jpg", "bb.jpg").start();
        new CopyFile("D:\\temp\\c.jpg", "cc.jpg").start();
    }
}

class CopyFile extends Thread {
    File oldFile;
    File newFile;

    public CopyFile(String oldFile, String newFile) {
        this.oldFile = new File(oldFile);
        this.newFile = new File(newFile);
    }

    @Override
    public void run() {
        // 复制文件操作
        try (FileInputStream in = new FileInputStream(oldFile);
             FileOutputStream out = new FileOutputStream(newFile)) {
            // 边读边写
            int readCount;
            byte[] bytes = new byte[1024];
            // 用来做累加的
            double temp = 0;
            long length = oldFile.length();
            while ((readCount = in.read(bytes)) != -1) {
                // write
                out.write(bytes);
                temp += readCount;
                double present = temp / length * 100;
                System.out.println(oldFile + "复制了:" + String.format("%.2f", present) + "%");
            }
            System.out.println(oldFile + "复制完成!");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
